package nl.hva.web.mvcwebapp.controllers;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import nl.hva.web.mvcwebapp.models.Course;
import nl.hva.web.mvcwebapp.service.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import nl.hva.web.mvcwebapp.doa.AssignmentDAO;
import nl.hva.web.mvcwebapp.doa.ChapterDAO;
import nl.hva.web.mvcwebapp.doa.CourseDAO;
import nl.hva.web.mvcwebapp.doa.UserDAO;
import nl.hva.web.mvcwebapp.models.Assignment;
import nl.hva.web.mvcwebapp.models.Chapter;
import nl.hva.web.mvcwebapp.models.News;
import nl.hva.web.mvcwebapp.models.User;

public class AssignmentController extends HttpServlet {

    private String address = "/assignments.jsp";
    ArrayList paths_jsp = new ArrayList();
    ArrayList paths = new ArrayList();

    /*
     * HTTP GET request
     */
    public ArrayList getPaths() {
        ArrayList paths = new ArrayList();
        paths.add(0, "/chapter/assignments/admin");
        paths.add(1, "/chapter/assignments");
        paths.add(2, "/chapter/assignments/edit");
        paths.add(3, "/chapter/assignments/new");
        paths.add(4, "/chapter/assignments/delete");
        return paths;
    }

    public ArrayList getPathsJSP() {
        ArrayList paths_jsp = new ArrayList();
        paths_jsp.add(0, "/assignment/assignments.jsp");
        paths_jsp.add(1, "/assignment/assignments_user.jsp");
        paths_jsp.add(2, "/assignment/assignment_edit.jsp");
        paths_jsp.add(3, "/assignment/assignment_edit.jsp");
        paths_jsp.add(4, "/assignment/assignments.jsp");


        return paths_jsp;
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        boolean redirect = false;
        ArrayList paths = getPaths();
        ArrayList paths_jsp = getPathsJSP();
        String path = request.getServletPath();


        for (int i = 0; i < paths.size(); i++) {
            try {
                if (path.equals(paths.get(i).toString())
                        || path.substring(0, path.toString().length() - 1).equals(paths.get(i).toString())) {
                    // /chapter/assignments/admin
                    System.out.println(i);
                    if (i == 0) {
                        int chapterid = Integer.parseInt(request.getParameter("chapterid"));
                        Chapter chapter = ChapterDAO.getChapter(chapterid);
                        List<Assignment> assignmentlist = AssignmentDAO.getAllAssignments(chapter.getChapterid());
                        request.setAttribute("chapter", chapter);
                        request.setAttribute("assignments", assignmentlist);
                        request.setAttribute("amountchapters", assignmentlist.size());
                    }// /chapter/assignments/
                    else if(i == 1){
                        int chapterid = Integer.parseInt(request.getParameter("chapterid"));
                        Chapter chapter = ChapterDAO.getChapter(chapterid);
                        List<Assignment> assignmentlist = AssignmentDAO.getAllAssignments(chapter.getChapterid());
                        for (int j = 0; j < assignmentlist.size(); j++) {
                            Hibernate.initialize(assignmentlist.get(j).getAssignmentsanswer());
                        }
                        request.setAttribute("chapter", chapter);
                        request.setAttribute("assignments", assignmentlist);
                        request.setAttribute("amountchapters", assignmentlist.size());
                    }// /assignment/edit
                    else if (i == 3) {
                        int assignmentid = Integer.parseInt(request.getParameter("assignment"));
                        Assignment assignment = AssignmentDAO.getAssignment(assignmentid);
                        request.setAttribute("assignment", assignment);
                        request.setAttribute("pageTitle", "Edit Chapter");
                        request.setAttribute("buttonTitle", "Edit");
                    } // /chapter/delete
                    else if (i == 4) {
                        int assignmentid = Integer.parseInt(request.getParameter("assignmentid"));
                        int chapterid = Integer.parseInt(request.getParameter("chapterid"));
                        AssignmentDAO.deleteAssignment(assignmentid);
                        Chapter chapter = ChapterDAO.getChapter(chapterid);
                        redirect = true;
                        System.out.println("delete");
                        response.sendRedirect(response.encodeRedirectURL("../chapter/assignments/admin?chapterid="+chapter.getChapterid()));
                    }
                    System.out.println("LOLLLL");
                    address = paths_jsp.get(i).toString();
                }
            } catch (Exception E) {
            }
        }

        request.setAttribute("path", path);
        if(!redirect){
            RequestDispatcher dispatcher = request.getRequestDispatcher(address);
            dispatcher.forward(request, response);
        }
        
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        ArrayList paths = getPaths();
        ArrayList paths_jsp = getPathsJSP();
       // Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        //Transaction tx = session.beginTransaction();
        String path = request.getServletPath();
        for (int i = 0; i < paths.size(); i++) {
            try {
                if (path.equals(paths.get(i).toString())
                        || path.substring(0, path.toString().length() - 1).equals(paths.get(i).toString())) {
                    address = paths_jsp.get(i).toString();
                    // ---------------- /assignment/edit --------------------
                    if (i == 2) {
                        int chapterid = Integer.parseInt(request.getParameter("chapterid"));
                        Chapter chapter = ChapterDAO.getChapter(chapterid);
                        AssignmentDAO.updateAssignment(request, chapter);
                        response.sendRedirect(response.encodeRedirectURL("../chapter/assignments/admin?chapterid="+chapterid));
                    }// --------------- /assignment/new --------------------
                    else if (i == 3) {
                        int chapterid = Integer.parseInt(request.getParameter("chapterid"));
                        Chapter chapter = ChapterDAO.getChapter(chapterid);
                        AssignmentDAO.insertAssignment(request, chapter);
                        response.sendRedirect(response.encodeRedirectURL("../../chapter/assignments/admin?chapterid="+chapterid));
                    }
                    
                }


            } catch (Exception E) {
            }
        }

    }
}
